<?php

Class SaleDetailResult extends CI_Model {

    function get_list() {


        $this->db->select('*');
        $this->db->from('sale_detail_result');

        if ($this->input->post('search_type')) {
            $search_type = $this->input->post('search_type');
            $search = $this->input->post('search');
            $this->db->like($search_type, $search);
        }
        ////$this->db->limit($this->config->item('number_of_rows'), $limit);
        $this->db->order_by("id", "desc");
        $query = $this->db->get();

        if ($query->num_rows() >= 1) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function get_first_where($wheres) {
        $this->db->select('*');
        $this->db->from('sale_detail_result');

        foreach ($wheres as $key => $value) {
            $this->db->where($key, $value);
        }
        //$this->db->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    function get($id) {
        $query = $this->db->get_where('sale_detail_result', array('id' => $id));
        return $query->row_array();
    }

    function insert($newRow) {
        if ($this->db->insert('sale_detail_result', $newRow)) {
            return "Đã thêm!";
        } else {
            return "Unable to add";
        }
    }

    function update($row) {
        $this->db->where('id', $row['id']);
        $this->db->update('sale_detail_result', $row);
        return "Đã cập nhật!";
    }

    function delete($id) {
        $this->db->delete('sale_detail_result', array('id' => $id));
        return "Đã xóa!";
    }

    function get_wheres($wheres) {
        $this->db->select('*');
        $this->db->from('sale_detail_result');

        foreach ($wheres as $key => $value) {
            $this->db->where($key, $value);
        }

        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    function import($results, $eid) {
        // xóa dữ liệu sp cũ của đợt đánh giá hiện tại 
        $this->db->delete('sale_detail_result', array('eid' => $eid));

        foreach ($results as $key => $eachResult) {
            if ($key != 0) {
//                echo "<pre>";
//                print_r($singlequestion);

                $insert_data = array(
                    'user_code' => $eachResult['0'],
                    'eid' => $eid,
                    'score' => $eachResult['1']
                );

                $this->db->insert('sale_detail_result', $insert_data);

//                if ($this->db->insert('qbank', $insert_data)) {
//                    $qid = $this->db->insert_id();
//                    $optionkeycounter = 3;
//                    for ($i = 1; $i <= 10; $i++) { // 10 option
//                        if ($singlequestion[$optionkeycounter] != "") {
//                            if ($singlequestion['2'] == $i) {
//                                $correctoption = '1';
//                            } else {
//                                $correctoption = 0;
//                            }
//                            $insert_options = array(
//                                "qid" => $qid,
//                                "option_value" => $singlequestion[$optionkeycounter],
//                                "institute_id" => $institute_id,
//                                "score" => $correctoption
//                            );
//                            $this->db->insert("q_options", $insert_options);
//                            $optionkeycounter++;
//                        }
//                    }
//                }
            }
        }
    }

}
